package com.gouaixin.bee.beeframework2.dao;

import android.content.Context;
import android.util.Log;

import com.gouaixin.bee.beeframework2.tool.mLog;
import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.bean.ZhenxinhuaMsg;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/4/7.
 */
//真心话对象数据库操作器
public class ZhenxinhuaMsgDao {

    public static void add(Context context, ZhenxinhuaMsg bean){
        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

            db.saveBindingId(bean);

            mLog.e("添加ZhenxinhuaMsg:成功:" + new Gson().toJson(bean));


        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }
    }

    public static void remove(Context context, ZhenxinhuaMsg bean){
        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

            int _id=bean.get_id();
            mLog.e("_id=" + _id);

            db.delete(Selector.from(ZhenxinhuaMsg.class).where(WhereBuilder.b("_id", "=",_id)));
//            db.saveOrUpdate(bean);

            mLog.e("删除ZhenxinhuaMsg:成功:" + new Gson().toJson(bean));

        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }
    }

    public static void update(Context context, ZhenxinhuaMsg bean){
        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);


            int _id=bean.get_id();
            mLog.e("_id=" + _id);

//            db.delete(Selector.from(ZhenxinhuaMsg.class).where(WhereBuilder.b("_id", "=",_id)));
            db.saveOrUpdate(bean);

            mLog.e("更新ZhenxinhuaMsg:成功:" + new Gson().toJson(bean));


        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }
    }





    public static List<ZhenxinhuaMsg> query(Context context){
        List<ZhenxinhuaMsg> list =new ArrayList<ZhenxinhuaMsg>();

        try {
            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

             list = db.findAll(Selector.from(ZhenxinhuaMsg.class));//.where(WhereBuilder.b("name", "=", "child' name")));
            mLog.e(new Gson().toJson(list));

        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }

        return list;
    }

    public static List<ZhenxinhuaMsg> queryWithId(Context context,String jid,String cid){
        List<ZhenxinhuaMsg> list =new ArrayList<ZhenxinhuaMsg>();

        try {

            //DbUtils db = DbUtils.create(this.getActivity(), "/sdcard/", "test.db");
            DbUtils db = DbUtils.create(context);
            db.configAllowTransaction(true);
            db.configDebug(true);

            mLog.e("查询的jid=" + jid);

            mLog.e("查询的cid=" + cid);

            list = db.findAll(Selector.from(ZhenxinhuaMsg.class).where(WhereBuilder.b("jid", "=",jid).and("cid","=",cid)));//.where("jid","=",jid).and("cid","=",cid));//.where(WhereBuilder.b("name", "=", "child' name")));

            mLog.e("查询ZhenxinhuaMsg:成功:" + new Gson().toJson(list));

//            list = db.findAll(Selector.from(ZhenxinhuaMsg.class).where(WhereBuilder.b("jid", "=", jid).and("cid","=",cid)));//.where("jid","=",jid).and("cid","=",cid));//.where(WhereBuilder.b("name", "=", "child' name")));
//            list = db.findAll(Selector.from(ZhenxinhuaMsg.class));//.where("jid","=",jid).and("cid","=",cid));//.where(WhereBuilder.b("name", "=", "child' name")));
//            Log.e("db",new Gson().toJson(list));

        } catch (DbException e) {
            Log.e("db",e.getMessage());
        }

        return list;
    }





}
